package com.jmu.edu.IMApplication;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.jmu.edu.IMApplication.bean.GestureAndFaceResult;
import com.jmu.edu.IMApplication.bean.Match;
import com.jmu.edu.IMApplication.fragment.ContactFragment;
import com.jmu.edu.IMApplication.fragment.MatchFragment;
import com.jmu.edu.IMApplication.fragment.MessageFragment;
import com.jmu.edu.IMApplication.fragment.SelectTagFragment;
import com.jmu.edu.IMApplication.fragment.ShowTagFragment;
import com.jmu.edu.IMApplication.gen.ChatRecordDao;
import com.jmu.edu.IMApplication.gen.DaoMaster;
import com.jmu.edu.IMApplication.gen.DaoSession;
import com.jmu.edu.IMApplication.net.bean.FriendInvite;
import com.jmu.edu.IMApplication.net.bean.Identify;
import com.jmu.edu.IMApplication.net.bean.UserIdentify;
import com.jmu.edu.IMApplication.utils.WillowUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.model.UserInfo;
import jackmego.com.jieba_android.JiebaSegmenter;
import okhttp3.OkHttpClient;

public class MyApplication extends Application {

    private static UserInfo user = null;
    public static RequestQueue requestQueue;
    public static OkHttpClient client;
    private static List<Identify> identifyList;
    private static List<UserIdentify> userIdentifyList;
    private static List<GestureAndFaceResult> gestureAndFaceResultList = new ArrayList<>();

    public static List<Identify> getIdentifyList() {
        return identifyList;
    }

    public static void setIdentifyList(List<Identify> identifyList) {
        MyApplication.identifyList = identifyList;
    }

    public static List<UserIdentify> getUserIdentifyList() {
        return userIdentifyList;
    }

    public static void setUserIdentifyList(List<UserIdentify> userIdentifyList) {
        MyApplication.userIdentifyList = userIdentifyList;
    }

    public static List<GestureAndFaceResult> getGestureAndFaceResultList() {
        return gestureAndFaceResultList;
    }

    public static void setGestureAndFaceResultList(List<GestureAndFaceResult> gestureAndFaceResultList) {
        MyApplication.gestureAndFaceResultList = gestureAndFaceResultList;
    }

    public static UserInfo getUser() {
        return user;
    }

    private static DaoSession daoSession;
    private static ChatRecordDao chatRecordDao;

    public static ChatRecordDao getChatRecordDao() {
        return chatRecordDao;
    }

    public static void setUser(UserInfo user) {
        MyApplication.user = user;
    }

    public static OkHttpClient getClient() {
        return client;
    }

    private static List<Fragment> fragmentList;

    public static List<Fragment> getFragmentList() {
        return fragmentList;
    }

    private static List<FriendInvite> friendInviteList;

    public static void setFriendInviteList(List<FriendInvite> friendInviteList) {
        MyApplication.friendInviteList = friendInviteList;
    }

    public static List<FriendInvite> getFriendInviteList() {
        return friendInviteList;
    }

    public static void setFragmentList(List<Fragment> fragmentList) {
        MyApplication.fragmentList = fragmentList;
    }

    public static FragmentManager fragmentManager;
    public static int ScreenWidth;
    public static int ScreenHeight;

    @Override
    public void onCreate() {
        super.onCreate();
        setupDatabase();
        Log.i("IMDebugApplication", "init");
        WillowUtils.setDebugMode(true);
        WillowUtils.init(getApplicationContext(), true);
        JiebaSegmenter.init(getApplicationContext());
        // 创建一个要放在TabLayout里面的Fragment的列表，按加入顺序展示
        fragmentList = new ArrayList<>();
        fragmentList.add(new MessageFragment());
        fragmentList.add(new ContactFragment());
        fragmentList.add(new MatchFragment());



        //注册全局事件监听类
        WillowUtils.registerEventReceiver(new GlobalEventListener(getApplicationContext()));

        requestQueue = Volley.newRequestQueue(getApplicationContext());
        // 创建okhttp的client
        client = new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)//设置超时时间
                .readTimeout(60, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(60, TimeUnit.SECONDS)//设置写入超时时间
                .build();

    }


    public static RequestQueue getHttpQueue() {
        return requestQueue;
    }

    private void setupDatabase() {
        // 创建数据库vhr.db
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "student.db", null);
        // 获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        // 获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        // 获取Dao对象管理者
        daoSession = daoMaster.newSession();
        chatRecordDao = daoSession.getChatRecordDao();

    }
}
